<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:46
 */
class Base extends MY_Admin_Controller
{
    public function sms_list()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	$limit = 10;
    	
    	$where = array('type'=>MESSAGE_SEND_LOG_SMS);
    	
        $this->load->model('message/Model_message_send_log','msl');
        $total_rows = $this->msl->count($where);
        
        //分页
        $this->load->library('pagination');
         
        $config['total_rows'] = $total_rows;
        $config['per_page'] = $limit;
        $config['base_url'] = '/message/Base/sms_list';
         
        $this->pagination->initialize($config);
        $show_page = $this->pagination->create_links();
        $offset = ($page-1) * $config['per_page'];
        
        $lists = $this->msl->lists($offset, $limit, $where);

        $data = array(
            'lists' => $lists,
            'show_page'   => $show_page,
        	'total_rows'  => $config['total_rows'],
        	'type'=>MESSAGE_SEND_LOG_SMS,
        );

        $this->load->view('admin/message/base/index', $data);
    }
    
    public function push_list()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;
    	$limit = 10;
    	 
    	$where = array('type'=>MESSAGE_SEND_LOG_PUSH);
    	 
    	$this->load->model('message/Model_message_send_log','msl');
    	$total_rows = $this->msl->count($where);
    
    	//分页
    	$this->load->library('pagination');
    	 
    	$config['total_rows'] = $total_rows;
    	$config['per_page'] = $limit;
    	$config['base_url'] = '/message/Base/push_list';
    	 
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();
    	$offset = ($page-1) * $config['per_page'];
    
    	$lists = $this->msl->lists($offset, $limit, $where);
    
    	$data = array(
    			'lists' => $lists,
    			'show_page'   => $show_page,
    			'total_rows'  => $config['total_rows'],
    			'type'=>MESSAGE_SEND_LOG_PUSH,
    	);
    
    	$this->load->view('admin/message/base/index', $data);
    }

    
    public function sms_send()
    {
        $this->load->view('admin/message/base/sms_send');
    }
    
    
    public function push_send()
    {
    	$push_way       = intval($this->input->get_post('push_way'));
    	$identification = trim($this->input->get_post('identification'));
    	$content        = trim($this->input->get_post('content'));


    	if($content)
    	{
    		$identification = explode(',', $identification);
    		$this->load->library('push/push');
    	
    		$param = array
    		(
                'send_type' => PUSH_SEND_TYPE_NOTIFICATION,
                'content'   => $content,
                'extra'     => array('goto_page' => 'home'),
    		);
    		
    		if($push_way == 1)
    		{
    			$param['alias'] = $identification;
    		}
    		
    		$rs = $this->push->factory()->send($param);
    	
    		$this->load->helper('url');
    		redirect(url('admin').'message/Base/push_list');
    		exit;
    	}
    	
    	$this->load->view('admin/message/base/push_send');
    }


    /**
     * 删除道具
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $mid = (int)$this->input->get_post('mid');

            if (!$mid)
            {
                $response = error(140001);
                break;
            }

            $this->load->model('message/model_message','message');

            $res = $this->message->delete(array('mid' => $mid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }
}